package cn.skylark.wcm.common.jwt;

import cn.skylark.wcm.common.redis.RedisUtils;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * JwtConfig
 *
 * @author chuhl
 * @date 2018/1/8
 */
@Configuration
@EnableTransactionManagement
public class JwtConfig  implements EnvironmentAware{
    private static final Logger logger = LoggerFactory.getLogger(JwtConfig.class);

    /**
     * RelaxedPropertyResolver对象
     */
    private RelaxedPropertyResolver propertyResolver;
    /**
     * RedisConnectionFactory对象
     */
    private RedisConnectionFactory redisConnectionFactory;
    /**
     * REDIS实例
     */
    private RedisUtils redisUtils;

    /**
     * 配置文件对于配置项取得
     *
     * 1:获取applicatoin.yml中标签为spring.redis的属性项目
     * 2:初始化RedisConnectionFactory对象
     * 3:生成redisTemplate对象
     * 4:实例化RedisUtil对象
     * @param env org.springframework.core.env.Environment
     */
    @Override
    public void setEnvironment(Environment env) {
        logger.info( "获取Redis服务器环境变量开始" );
        this.propertyResolver = new RelaxedPropertyResolver(env, "spring.redis.");
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(  );
        jedisConnectionFactory.setHostName( propertyResolver.getProperty( "host" ) );
        jedisConnectionFactory.setPort( Integer.valueOf( propertyResolver.getProperty( "port" ) ) );
        jedisConnectionFactory.setDatabase(  Integer.valueOf( propertyResolver.getProperty( "database" ) ) );
        jedisConnectionFactory.setPassword(  propertyResolver.getProperty( "password" ) );
        jedisConnectionFactory.setTimeout(  Integer.valueOf( propertyResolver.getProperty( "timeout" ) ) );
        jedisConnectionFactory.afterPropertiesSet();
        redisConnectionFactory=jedisConnectionFactory;
        StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer =
                new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility( PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        redisUtils = new RedisUtils();
        redisUtils.redisTemplate=template;
        logger.info( "获取Redis服务器环境变量结束" );
    }

    /**
     * JWT过滤器
     * 1:设置验证访问路径
     * 2:调用JWT鉴权过滤器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean jwtFilter() {
        final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        String[] urlPatten=new String[]{"/users/*","/roles/*","/tenans/*","/resources/*","/recycle/*","/filemanager/*","/pagemanager/*","/tenanproperty/saveTenanProperty","/userrole/*","/userinfo","/userpwd"};
        registrationBean.setFilter(new JwtAuthenticationFilter(urlPatten,redisUtils));
        return registrationBean;
    }
}
